using System;
using System.Collections.Generic;
using System.Text;

namespace Wrath.Xml.Objects
{
    public class ControlBounds : IControlPosition
    {
        #region Properties

        private int x;

        public int X
        {
            get { return x; }
            set { x = value; }
        }

        private int y;

        public int Y
        {
            get { return y; }
            set { y = value; }
        }

        private int width;

        public int Width
        {
            get { return width; }
            set { width = value; }
        }

        private int height;

        public int Height
        {
            get { return height; }
            set { height = value; }
        }

        #endregion

        public ControlBounds(Element element)
        {
            string[] bounds = element.Value.Split(',');
            if (bounds.Length < 4) return;

            try { x = Convert.ToInt32(bounds[0]); }
            catch { x = 0; }

            try { y = Convert.ToInt32(bounds[1]); }
            catch { y = 0; }

            try { width = Convert.ToInt32(bounds[2]); }
            catch { width = 0; }

            try { height = Convert.ToInt32(bounds[3]); }
            catch { height = 0; }
        }

        public ControlBounds(int x, int y, int width, int height)
        {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        #region IElementContainer Members

        private Element sourceElement;
        public Element SourceElement
        {
            get { return sourceElement; }
        }

        public void Commit()
        {
            sourceElement = new Element("bounds");

            sourceElement.Value = String.Format("{0},{1},{2},{3}", x, y, width, height);
        }

        #endregion
    }
}
